
package commands;

import application.Core;

import java.io.File;

/**
 *	This class implements command cd.
 * 
 * @author Michal Mourek
 */

public class Cd extends AbstractCommand{
	
	/**
	 * Main method of this program.
	 * It changes current working directory.
	 *
	 * @param parameters List of parameters entered by user in command line.
	 * @param arguments List of arguments entered by user in command line.
	 * @param id Process ID.
	 * @param core reference to the core
	 * @author Michal Mourek
	 */
	@Override
	public void main(String[] parameters, String[] arguments, int id, Core core) {
		String workingFolder = core.getDirName(id);
		File verifyPath = new File(workingFolder);
		if (parameters.length == 0){
			if (arguments.length != 0){			
				if (arguments[0].equals("..")){
					 File newWorkingFolder = new File(verifyPath.getParent());
					 if (newWorkingFolder.isDirectory()){
						 core.changeDir(id,newWorkingFolder.getPath());
					 }
					 else {
						 core.changeDir(id,core.getDirName(id));
					 }
				}
				else if(arguments[0].equals(".")) {
					//nothing
				}
				else {
					 File changePath = new File(arguments[0]);
					 if(changePath.isDirectory()){
						 core.changeDir(id,changePath.getPath());
					 } else{
						 changePath = new File(workingFolder + "/" + arguments[0]);
						 if(changePath.isDirectory()){
							 core.changeDir(id,changePath.getPath());
						 } else{
							 String[] invArgs = invalidArguments().split("\n");
								for (String string : invArgs) {
									core.write(id, string);
									try {
										Thread.sleep(50);
									} catch (InterruptedException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
						 }
					 }
				}			
			} else {
				String[] invArgs = invalidArguments().split("\n");
				for (String string : invArgs) {
					core.write(id, string);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		} else {
			for (int i = 0; i < parameters.length; i++) {
				char parameter = parameters[i].charAt(1);
				if (parameter == 'h'){
					String[] help = help().split("\n");
					for (String string : help) {
						core.write(id, string);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				} else{
					String[] invPar = invalidParameters().split("\n");
					for (String string : invPar) {
						core.write(id, string);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					break;
				}
			}
		}
	}
	/**
	 * Print help for this command.
	 *
	 * @author Michal Mourek
	 */
	@Override
	public String help() {
		return ("NAME: cd - change working directory \n SYNOPSYS: cd [OPTION] \n \n Description: \n   -h      print help \n");
	}
	
	/**
	 * Print warning message, when parameter is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidParameters(){
		return ("Parameters is not correct \n Read help cd - h \n");
	}
	
	/**
	 * Print warning message, when argument is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidArguments(){
		return ("Arguments is not correct \n Read help cd - h \n");
	}
}
